package data

import (
	"net/http"
	"sync"
	"time"

	"github.com/gorilla/websocket"
	"github.com/libp2p/go-libp2p-core/host"
	dht "github.com/libp2p/go-libp2p-kad-dht"
)

var (
	LocalPeers               = &sync.Map{}
	SelfOsName               = ""
	SelfIp                   = ""
	SelfClientName           = ""
	ControlPort              = 0
	SelfP2PID                = ""
	SelfP2PIP                = ""
	LibP2PHost     host.Host = nil
	DHT            *dht.IpfsDHT
)

type LocalPeer struct {
	Name             string    `json:"name"`
	Status           int       `json:"status"`           //状态，1已配置，2已连接，3连接失败，4已删除
	Mode             string    `json:"mode"`             //传输类型，p2p直连，relay中继
	InnerIp          string    `json:"innerIp"`          //内部ip地址
	ConfigAddTime    time.Time `json:"configAddTime"`    //配置添加时间
	ConfigUpdateTime time.Time `json:"configUpdateTime"` //配置更新时间
	ConfigDelTime    time.Time `json:"configDelTime"`    //配置删除时间
	OsName           string    `json:"osName"`           //系统名称
	ControlPort      string    `json:"controlPort"`      //控制端口
	FailCount        int       `json:"failCount"`        //心跳失败次数
	LibP2PId         string    `json:"libP2PId"`         //libp2p id
	ClientId         string    `json:"clientId"`         //客户端ID
}

func (l *LocalPeer) StatusToStr() string {
	if l.Status == 1 {
		return "已配置"
	} else if l.Status == 2 {
		return "已连接"
	} else if l.Status == 3 {
		return "连接失败"
	} else if l.Status == 4 {
		return "已删除"
	} else {
		return "未知"
	}
}

type Token struct {
	Server string `json:"server"` //服务端地址
	Aid    string `json:"aid"`    //服务端分配的p2p区域ID
	Cid    string `json:"cid"`    //服务端分配的客户端ID
	CIp    string `json:"cip"`    //服务端分配的客户端IP
	Cn     string `json:"cn"`     //服务端分配的客户端wg网卡名称
}

type Connetion struct {
	Con     *websocket.Conn
	Mutex   sync.Mutex
	Header  http.Header
	Address string
}

type TransmitParams struct {
	Operate        string `json:"operate"`
	EncryptionData string `json:"encryptionData"`
}

type P2PSocketParams struct {
	MType string           `json:"mType"`
	My    P2PInterfaceInfo `json:"my,omitempty"`
	Peers []P2PPeerInfo    `json:"peers,omitempty"`
	Relay RelayInfo        `json:"relay,omitempty"`
}

type P2PInterfaceInfo struct {
	P2PPeerInfo
}

type P2PPeerInfo struct {
	Name string `json:"name,omitempty"`
	Mode string `json:"mode,omitempty"` //p2p relay
	IIp  string `json:"iIp,omitempty"`  //内部ip地址
	On   string `json:"on,omitempty"`   //系统名称
	Cp   string `json:"cp,omitempty"`   //控制端口
	Id   string `json:"id,omitempty"`   //libp2p peer id
	Cid  string `json:"cid,omitempty"`  //客户端id
}

type RelayInfo struct {
	Id   string `json:"id,omitempty"`
	Addr string `json:"addr,omitempty"`
}
